<?php
/*
 * View class (template)
 * - will generate the proper view and its header and footer
 *
 * @author Andreas Psaros, presis.se
 */

class View {

    protected $entity;
    protected $action;

    protected $variables = array();
    protected $css = array();
    
    protected $pageTitle = null;
    protected $error = false;

    protected $renderHeader = true;
    protected $renderSidebar = true;
    protected $renderSub = true;

    function __construct($entity,$action) {
        $this->entity = strtolower($entity);
        $this->action = $action;
    }

    /*
     * Set a variable to the field array
     */
    function set($name, $value) {
        $this->variables[$name] = $value;
    }

    /*
     * Handles errors, invoke the correct error
     */
    function error($type){
        // force to not render on controller exit
        $this->error = $type;
    }

    /*
     * Set boolean to not render header/footer
     * - functional for e.g. AJAX pages
     */
    function noHeader(){
        $this->renderHeader = false;
    }

    /*
     * Set boolean to not render rightbar/leftbar
     */
    function noSidebar(){
        $this->renderSidebar = false;
    }

    /*
     * Set boolean to not render rightbar/leftbar
     */
    function noSub(){
        $this->renderSub = false;
    }


    /*
     * Add a CSS file to array, should always be checked in header
     */
    function addCss($filename){
        $this->css[count($this->css)] = $filename;
    }

    /*
     * Set page title in global header
     */
    function setPageTitle($pageTitle){
        $this->pageTitle = $pageTitle;
    }
    

    /*
     * Renders the view with current includes
     * - called from parent controller on destruct
     */
    function render() {

        // init variables
        $html = new Html();
        $pageTitle = $this->pageTitle;
        extract($this->variables);

        $css = $this->css;
        
        // include header
        // - check in view of current entity, if no match then include global
        if($this->renderHeader){
            if (file_exists('application/view/' . $this->entity . '/header.php')) {
                include ('application/view/' . $this->entity . '/header.php');
            } else {
                include ('application/view/header.php');
            }
        }


        // check so that we do not have any error, then render content
        if(!$this->error){

            // include left bar (if any)
            // - check in view of current entity, if no match then include global
            if($this->renderSidebar){
                if (file_exists('application/view/' . $this->entity . '/leftbar.php')) {
                    include ('application/view/' . $this->entity . '/leftbar.php');
                } else {
                    if (file_exists('application/view/leftbar.php')) {
                        include ('application/view/leftbar.php');
                    }
                }
            }


            // include message
            if (file_exists('application/view/message.php'))
                include ('application/view/message.php');



            // add sub-header and sub-leftbar
            if($this->renderSub){
                // sub-header
                if (file_exists('application/view/' . $this->entity . '/sub-header.php'))
                    include ('application/view/' . $this->entity . '/sub-header.php');

                // sub-leftbar
                if (file_exists('application/view/' . $this->entity . '/sub-leftbar.php'))
                    include ('application/view/' . $this->entity . '/sub-leftbar.php');
            }


            // include view of current entity
            include ('application/view/' . $this->entity . '/'. $this->action . '.php');


            // add sub-rightbar and sub-footer
            if($this->renderSub){
                // sub-leftbar
                if (file_exists('application/view/' . $this->entity . '/sub-rightbar.php'))
                    include ('application/view/' . $this->entity . '/sub-rightbar.php');

                // sub-footer
                if (file_exists('application/view/' . $this->entity . '/sub-footer.php'))
                    include ('application/view/' . $this->entity . '/sub-footer.php');
            }



            // include right bar (if any)
            // - check in view of current entity, if no match then include global
            if($this->renderSidebar){
                if (file_exists('application/view/' . $this->entity . '/rightbar.php')) {
                    include ('application/view/' . $this->entity . '/rightbar.php');
                } else {
                    if (file_exists('application/view/rightbar.php')) {
                        include ('application/view/rightbar.php');
                    }
                }
            }

        }

        // .. else on error, render error message
        else {
            if (file_exists('application/view/error/' . $this->error . '.php')) {
                include ('application/view/error/' . $this->error . '.php');
            }
        }

        // include footer
        // check in view of current entity, if no match then include global
        if($this->renderHeader){
            if (file_exists('application/view/' . $this->entity . '/footer.php')) {
                include ('application/view/' . $this->entity .  '/footer.php');
            } else {
                include ('application/view/footer.php');
            }
        }
        
    }

}

?>